#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <unistd.h>

int get_listen_fd(char*);

int main(int argc,char* argv[])
{
  if(argc != 2)
  {
    perror("plread input port\n");
    return -1;
  }
  int tcp_fd = get_listen_fd(argv[1]);


  close(tcp_fd);
  return 0;
}

int get_listen_fd(char* port)
{
  int tcp_fd = socket(AF_INET,SOCK_STREAM,0);
  if(tcp_fd < 0)
  {
    perror("socket fail");
    exit(-1);
  }

  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(atoi(port));
  inet_pton(AF_INET,"0.0.0.0",&server_addr.sin_addr);

  int ret = bind(tcp_fd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in));
  if(ret < 0)
  {
    perror("bind fail");
    exit(-1);
  }
  
  int on = 1;
  setsockopt(tcp_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));

  listen(tcp_fd,1);
  return tcp_fd;
}
